//
//  JCCataViewController.m
//  ABookOfCartoon
//
//  Created by 美国大狗 on 15-5-18.
//  Copyright (c) 2015年 Power Of Ants. All rights reserved.
//

#import "JCCataViewController.h"
#import "JCSeekPageModel.h"
#import "JCCataCollectionViewCell.h"
#import "JCCataModel.h"
#import "DDelicateOfCartoonViewController.h"
#import "MJRefresh.h"
#import "AFNetworking.h"

@interface JCCataViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)NSMutableDictionary *dataDic;
@property(nonatomic, retain)NSMutableDictionary *pageDic;


@property (nonatomic, retain)NSMutableArray *CartoonArr;
@property (nonatomic, retain)UICollectionView *collectionView;
@end

@implementation JCCataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.EXXtitle;

    [self createControllView];
    [self addFooter];
    [self addHeader];
    [self GetPersonList:0];
}
- (void)createControllView
{
//#error 固定值
    UICollectionViewFlowLayout *f = [[UICollectionViewFlowLayout alloc] init];
    f.itemSize = CGSizeMake((self.view.frame.size.width - 40) / 3, self.view.frame.size.height / 4 + 20);
    f.minimumLineSpacing = 10;
    f.minimumInteritemSpacing = 3;

    self.CartoonArr = [[NSMutableArray array] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 134) collectionViewLayout:f];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [f release];
    //以下为注册cell
    [_collectionView registerClass:[JCCataCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
}
//controllerView 方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JCCataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    JCCataModel *R2F = [[JCCataModel alloc] init];
    //************************************
    R2F.CatalogName = [[_dataArr objectAtIndex:indexPath.item] objectForKey:@"BookName"];
    R2F.CatalogOtherURL = [[_dataArr objectAtIndex:indexPath.item] objectForKey:@"BookIconOtherURL"];
    //************************************
    cell.model = R2F;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DDelicateOfCartoonViewController *jump = [[DDelicateOfCartoonViewController alloc] init];
//    jump.sb = _dataArr[indexPath.row];
    jump.BookDic = _dataArr[indexPath.row];
    [self.navigationController pushViewController:jump animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 卧槽

- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block JCCataViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.collectionView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        _nextCursor = 0;
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:0];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.collectionView headerBeginRefreshing];
    
    [_collectionView  headerEndRefreshing];
}

- (void)addFooter
{
    __block  JCCataViewController *w = self;
    [self.collectionView addLegendFooterWithRefreshingBlock:^{
        w.isUpLoading = YES;//标记为上拉操作
        
        
        _nextCursor ++;
        [w GetPersonList:w.nextCursor];
    }];
    

}

- (void)footerRefresh
{
    [self GetPersonList:self.nextCursor];
}
#pragma mark -- 使用AFN进行数据请求
-(void)GetPersonList:(NSInteger)nextCursor
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
   
    //****参数cursor的值: 第一页默认为0，下一页就是请求下来数据中next_cursor的值
    //****参数count 的值: 是指定一页显示多少条数据
    
    NSString *url_string = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=booklist&pageSize=20&cataID=%ld&state=0&order=0&pageIndex=%ld",_cataID, nextCursor];

    NSLog(@"url = %@",url_string);

    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        
//        NSDictionary *allDic = responseObject;
        self.dataDic = responseObject;
        self.dataArr = [_dataDic objectForKey:@"Booklist"];
        self.pageDic = [_dataDic objectForKey:@"PageInfo"];
        NSLog(@"使用AFN进行get请求 ===  %@",_dataDic);
        //记录下一页
//        self.nextCursor = [[_pageDic objectForKey:@"PageCount"]  integerValue];
        NSLog(@"nextCursor == %ld",self.nextCursor);
        

        
        
        [self.collectionView.footer endRefreshing];//结束底部刷新效果
        [self.collectionView reloadData];//刷新collectionview
        _collectionView.contentOffset = CGPointMake(0, 0);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
